import '../helper/dioHelper2.dart';

class Inventory {
  int ID;
  String SpaceName;
  String Guid;
  String TName;
  String TType;
  String TStyle;
  int SType;
  String Unit;
  double Amount;
  double Amount2;
  int State;
  String UserName;
  String Storage;
  String LDQM;
  String LDTM;
  String UserID;
  String CreateTM;
  String ScrapTM;
  String Remarks;

  Inventory(
      this.ID,
      this.SpaceName,
      this.Guid,
      this.TName,
      this.TType,
      this.TStyle,
      this.SType,
      this.Unit,
      this.Amount,
      this.Amount2,
      this.State,
      this.UserName,
      this.Storage,
      this.LDQM,
      this.LDTM,
      this.UserID,
      this.CreateTM,
      this.ScrapTM,
      this.Remarks);
  Inventory.fromJson(Map<String, dynamic> json)
      : ID = json["ID"],
        SpaceName = json["SpaceName"],
        Guid = json["Guid"],
        TName = json["TName"],
        TType = json["TType"],
        TStyle = json["TStyle"],
        SType = json["SType"],
        Unit = json["Unit"],
        Amount = json["Amount"] * 1.0,
        Amount2 = json["Amount2"] * 1.0,
        State = json["State"],
        UserName = json["UserName"],
        Storage = json["Storage"],
        LDQM = json["LDQM"],
        LDTM = json["LDTM"],
        UserID = json["UserID"],
        CreateTM = json["CreateTM"],
        ScrapTM = json["ScrapTM"],
        Remarks = json["Remarks"];
  Map<String, dynamic> toJson() => {
        'ID': ID,
        'SpaceName': SpaceName,
        'Guid': Guid,
        'TName': TName,
        'TType': TType,
        'TStyle': TStyle,
        'SType': SType,
        'Unit': Unit,
        'Amount': Amount,
        'Amount2': Amount2,
        'State': State,
        'UserName': UserName,
        'Storage': Storage,
        'LDQM': LDQM,
        'LDTM': LDTM,
        'UserID': UserID,
        'CreateTM': CreateTM,
        'ScrapTM': ScrapTM,
        'Remarks': Remarks,
      };

  static Future<bool> Add(Inventory x1) async {
    var response = await DioHelper.Post("/Inventory/Add", x1.toJson());
    if (response.statusCode == 200) return true;
    return false;
  }

  static Inventory GetEmpty()  {
    return Inventory(0, "", "", "", "0", "0", 0, "0", 0, 0, 0, "", "", "", "LDTM", "UserID", "CreateTM", "ScrapTM", "Remarks");
  }

  static Future<bool> Update(Inventory x1) async {
    var response = await DioHelper.Post("/Inventory/Update", x1.toJson());
    if (response.statusCode == 200) return true;
    return false;
  }

  static Future<bool> Delete(Inventory x1) async {
    var st = x1.toJson();
    var response = await DioHelper.Post("/Inventory/Delete", x1.toJson());
    if (response.statusCode == 200) return true;
    return false;
  }

  static Future<Inventory> Get(String where, dynamic data) async {
    var response = await DioHelper.Post("/Inventory/GetByWhere", {
      "Where": where,
      "obj": data,
    });

    if (response.statusCode == 200)
      return new Inventory.fromJson(response.data);
    return GetEmpty();
  }

  static Future<Inventory> GetByID(int ID) async {
    var response =
        await DioHelper.Get("/Inventory/GetByID?ID=" + ID.toString(), {});

    if (response.statusCode == 200) return new Inventory.fromJson(response.data);
    return GetEmpty();
  }

  static Future<List<Inventory>> GetList(
      int State, int pageIndex, int pageSize) async {
    var response = await DioHelper.Get(
        "/Inventory/GetList?st=$State&p1=$pageIndex&ps=$pageSize", {});
    var list1 = <Inventory>[];
    if (response.statusCode == 200) {
      for (var x in response.data) {
        list1.add(Inventory.fromJson(x));
      }
    }
    return list1;
  }

    static Future<List<Inventory>> GetListByType(
     int type, int State, int pageIndex, int pageSize) async {
    var response = await DioHelper.Get(
        "/Inventory/GetListByType?tp=$type&st=$State&p1=$pageIndex&ps=$pageSize", {});
    var list1 = <Inventory>[];
    if (response.statusCode == 200) {
      for (var x in response.data) {
        list1.add(Inventory.fromJson(x));
      }
    }
    return list1;
  }


  static Future<List<Inventory>> GetListByName(
    String nm) async {
    var response = await DioHelper.Get(
        "/Inventory/GetListByTName?nm=$nm", {});
    var list1 = <Inventory>[];
    if (response.statusCode == 200) {
      if(response.data==null) {
        return list1;
      }
      for (var x in response.data) {
        list1.add(Inventory.fromJson(x));
      }
    }
    return list1;
  }

  static Future<List<Inventory>> GetListNotQM(
    ) async {
    var response = await DioHelper.Get(
        "/Inventory/GetListNotQM", {});
    var list1 = <Inventory>[];
    if (response.statusCode == 200) {
      if(response.data==null)
      return list1;
      for (var x in response.data) {
        list1.add(Inventory.fromJson(x));
      }
    }
    return list1;
  }

    static Future<List<Inventory>> GetListNotReturn(
    ) async {
    var response = await DioHelper.Get(
        "/Inventory/GetListNotReturn", {});
    var list1 = <Inventory>[];
    if (response.statusCode == 200) {
      if(response.data==null)
      return list1;
      for (var x in response.data) {
        list1.add(Inventory.fromJson(x));
      }
    }
    return list1;
  }

  static Future<List<Inventory>> GetAllList() async {
    var response = await DioHelper.Get("/Inventory/GetAllList", {});
    var list1 = <Inventory>[];
    if (response.statusCode == 200) {
      for (var x in response.data) {
        list1.add(Inventory.fromJson(x));
      }
    }
    return list1;
  }
}
